<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Index extends Www_Controller
{

    function __construct()
    {
        parent::__construct();
    }

    public function index()
    {

        $this->load->model('banner_m');
        $banner['pc']     = $this->banner_m->select(['type' => 0]);
        $banner['mobile'] = $this->banner_m->select(['type' => 1]);
        if ($banner['pc']) {
            foreach ($banner['pc'] as $key => $val) {
                $banner['pc'][$key]['pic_url'] = str_replace('uploads/', "/uploads/", $val['pic_url']);
                $banner['pc'][$key]['pic_url'] = str_replace('//uploads/', "/uploads/", $banner['pc'][$key]['pic_url']);
            }
        }
        $data['banner'] = $banner;
        // 报价列表
        $this->load->model('product_m');
        $this->load->model('category_m');
        $shwo_category = $this->category_m->get_cates_by("all_category = 1");

        $shwo_category = implode(",", array_column($shwo_category, "id"));

        $page_list = $this->product_m->select("category in ({$shwo_category})", 4);

        if (!empty($page_list)) {
            $this->load->model('attachs_m');
            foreach ($page_list as $key => $value) {
                $page_list[$key]['product_img'] = base_url($value["img"]);;
            }
        }
        $data['hot'] = $page_list;
        // 新闻
        $this->load->model('account_m');
        $news         = $this->account_m->select("", 2, 0, "id,title,keyword,describe,create_time,img");
        $data['news'] = $news;

        // pp($data);exit();
        $this->load->view('index', $data);
    }

    /**
     * 关于我们
     */
    public function contact()
    {
        $data['rid'] = 5;
        $this->load->view('contact', $data);
    }

    public function jobs()
    {
        $data['rid'] = 5;
        $this->load->view('jobs', $data);
    }


    public function page($cate_id = 1)
    {
        // 获取简介
        $this->load->model('article_m');
        $page = $this->article_m->get_news_by_id($cate_id);
        if (!empty($page)) {
            $this->load->model('attachs_m');
            if (!empty($value['article_cover'])) {
                $access_key          = $value['article_cover'];
                $pic                 = $this->attachs_m->get_attachs_by($access_key);
                $pic_path            = base_url('uploads/article/' . $access_key . "/" . $pic[0]['file_name']);
                $page['article_img'] = $pic_path;
            } else {
                $page['article_img'] = base_url('public/wap/images/logo.png');;
            }
        }
        $data['page'] = $page;

        $data['id'] = $page['article_category'];
        // pp($data);
        // exit();
        $this->load->view('about', $data);
    }

    public function product($page = 1)
    {

        // $this->load->library('pagination');
        // $this->load->model('article_m');
        // $where = [];
        // //分页
        // $limit                      = 4;
        // $config['uri_segment']      = 2;
        // $config['use_page_numbers'] = true;
        // $config['total_rows']       = $this->article_m->get_all_articles_count(5);
        // $config['base_url']         = base_url('product');
        // $config['per_page']         = $limit;
        // $config['num_links']        = 1;
        // $start                      = ($page - 1) * $limit;
        // $this->pagination->initialize($config);
        // $data['pagination'] = $this->pagination->create_links();
        // $products           = $this->article_m->get_all_articles($start, $limit, 5);
        //
        //
        // if (!empty($products)) {
        //     $this->load->model('attachs_m');
        //     foreach ($products as $key => $val) {
        //         $access_key            = $val['article_cover'];
        //         $pic                  = $this->attachs_m->get_attachs_by($access_key);
        //         $products[$key]['img'] =  base_url('uploads/article/' . $access_key . "/" . $pic[0]['file_name']);
        //     }
        // }
        // $data["products"] = $products ?? [];
        // $data["id"]       = 5;

        // 报价列表
        $this->load->model('product_m');
        $this->load->model('category_m');
        $shwo_category = $this->category_m->get_cates_by("all_category = 1");

        $shwo_category = implode(",", array_column($shwo_category, "id"));

        $page_list = $this->product_m->select("category in ({$shwo_category})", 4);

        if (!empty($page_list)) {
            $this->load->model('attachs_m');
            foreach ($page_list as $key => $value) {
                $page_list[$key]['product_img'] = base_url($value["img"]);;
            }
        }
        $data["products"] = $page_list;
        $data["id"]       = 5;
        // pp($data);
        // exit();
        $this->load->view('product', $data);
    }

    /**
     * 资讯详情
     */
    public function product_show($id = 0)
    {
        // $this->load->model('article_m');
        // $this->load->model('attachs_m');
        // $this->load->model('category_m');
        // $article         = $this->article_m->get_news_by_id($id);
        // $data["article"] = $article ?? [];
        // // 分类
        // $cur_category         = $this->category_m->get_category_by_id($article['article_category']);
        // $data["cur_category"] = $cur_category ?? [];
        // // 图片
        // $access_key          = $article['article_cover'];
        // $imgs                 = $this->attachs_m->get_attachs_by($access_key);
        // $data['img'] = [];
        // if (!empty($imgs)) {
        //     foreach ($imgs as $key => $val) {
        //         $data['img'][] = base_url('uploads/article/' . $access_key . "/" . $val['file_name']);
        //     }
        // }
        //获取上一篇
        $this->load->model('product_m');
        $product                = $this->product_m->find($id);
        $product['product_img'] = base_url($product["img"]);;
        $data["product"] = $product ?? [];
        // // 分类
        $cur_category         = $this->category_m->get_category_by_id($product['category']);
        $data["cur_category"] = $cur_category ?? [];
        // pp($data);exit();
        $this->load->view('product_show', $data);
    }

    /**
     * 直通资讯
     */
    public function news($id = 1, $page = 1)
    {
        $where = ['deletetd' => 0];
        if ($id) {
            $where['category'] = $id;
        }
        $this->load->model('account_m');
        $this->load->library('pagination');
        $limit                      = 2;
        $config['uri_segment']      = 3;
        $config['use_page_numbers'] = true;
        $config['total_rows']       = $this->account_m->total($where);
        $config['base_url']         = base_url('news/' . $id . "/");
        $config['per_page']         = $limit;
        $config['num_links']        = 1;
        $start                      = ($page - 1) * $limit;
        $this->pagination->initialize($config);
        $data['pagination'] = $this->pagination->create_links();
        $field              = "id,category,title,describe,create_time,img";
        $data["news"]       = $this->account_m->select($where, $limit, $start, $field);
        $data["total"]      = $config['total_rows'];
        $data["id"]         = $id;

        $this->load->view('news', $data);
    }

    /**
     * 资讯详情
     */
    public function news_show($id = 0)
    {
        $this->load->model('account_m');
        $article         = $this->account_m->find($id);
        $data["article"] = $article ?? [];
        // 分类
        $cur_category         = $this->zixunCategory_m->find($article['category']);
        $data["cur_category"] = $cur_category ?? [];
        // pp($data);exit();
        $this->load->view('news_show', $data);
    }

    /**
     * 加入我们
     */
    public function join($cate_id = 4)
    {
        // 获取简介
        // 获取简介
        $this->load->model('article_m');
        $page = $this->article_m->get_news_by_id($cate_id);
        if (!empty($page)) {
            $this->load->model('attachs_m');
            if (!empty($value['article_cover'])) {
                $access_key          = $value['article_cover'];
                $pic                 = $this->attachs_m->get_attachs_by($access_key);
                $pic_path            = base_url('uploads/article/' . $access_key . "/" . $pic[0]['file_name']);
                $page['article_img'] = $pic_path;
            } else {
                $page['article_img'] = base_url('public/wap/images/logo.png');;
            }
        }
        $data['page'] = $page;

        $data['id'] = $page['article_category'];
        // pp($data);
        // exit();
        $this->load->view('join', $data);
    }

    public function join_label()
    {
        $this->load->view('join_label');
    }

    public function join_submit()
    {
        $post  = $this->input->post();
        $title = isset($post['name']) ? htmlspecialchars($post['name'], ENT_QUOTES) : false;
        $tel   = isset($post['mobile']) ? htmlspecialchars($post['mobile'], ENT_QUOTES) : false;
        $con   = isset($post['con']) ? htmlspecialchars($post['con'], ENT_QUOTES) : false;

        if (!$title) {
            echo json_encode(['status' => 0, 'msg' => '姓名不能为空']);
            exit();
        }
        if (!$tel) {
            echo json_encode(['status' => 0, 'msg' => '电话不能为空']);
            exit();
        }
        if (!$con) {
            echo json_encode(['status' => 0, 'msg' => '备注不能为空']);
            exit();
        }
        $properties = [
            'name' => $title,
            'tel'  => $tel,
            'con'  => $con,
            'time' => date("Y-m-d H:i:s", time()),
        ];
        // 新增
        $this->load->model('feedback_m');

        $properties = $this->feedback_m->insert($properties);
        if ($properties) {
            echo json_encode(['status' => 1, 'msg' => '申请成功']);
            exit();
        } else {
            echo json_encode(['status' => 0, 'msg' => '申请失败']);
            exit();
        }
    }
}
